from DatabaseConnector import connection
import TokenMatcher
from PorterStemmer import stem

class DiagDicGenerator:
    DiagnosisDic = []
    DiagDesc = ""
    IgnoreDic = []
    
    def __new__(cls, *args, **kargs):
        if cls.instance is None:
            cls.instance = object.__new__(cls, *args, **kargs)
        return cls.instance   
    
    def __init__(self):
        self.DiagnosisDic = []
        self.DiagDesc = ""
        self.IgnoreDic = []
        
    def _initDictionary(self):
        self._loadIgnoreDic()
        cursor = connection.cursor()
        cursor.execute("SELECT DiagnosisDescription from hosrep.diagnosis_codes_table")
        f = file("LexSCT/diag_dictionary.txt","w")
        for tuple in cursor.fetchall():
            self.DiagDesc = tuple[0]
            if self.IgnoreDic.count(self.DiagDesc) <> 0:
                continue
            else:
                print "Original diagnosis: " + self.DiagDesc
                DiagTerms_ls = self.DiagDesc.lower().split()
                for i in range(0, len(DiagTerms_ls)):
                    DiagTerms_ls[i] = DiagTerms_ls[i].strip(",\(\)'.\/\\:-")
                
                stoplist = TokenMatcher.load_stopwords()
                DiagTerms_ls = [stem(token) for token in DiagTerms_ls if token not in stoplist]
                print DiagTerms_ls
                for ((s, e), ids) in TokenMatcher.match_sentence_step(DiagTerms_ls):
                    print s, e, ids
                    term_str = ""
                    if e == s + 1:
                        term_str = DiagTerms_ls[s]
                    else:
                        term_str = DiagTerms_ls[s]
                        for i in range(s+1, e):
                            term_str = term_str + " " + DiagTerms_ls[i]
                    print "term: " + term_str  
                    if term_str == "" or self.DiagnosisDic.count(term_str) <> 0 : 
                        continue
                    self.DiagnosisDic.append(term_str)
                    term_str = term_str + ":\n"
                    f.write(term_str)
        f.close()  
                
    def _loadIgnoreDic(self):
        f= file("LexSCT/ignore_list","r")
        for line in f:
            self.IgnoreDic.append(line.strip())
    
    def _getDiagDict(self):
        f = file("LexSCT/diag_dictionary.txt","r")
        for terms in f:
#            print terms
            self.DiagnosisDic.append(terms.strip())
        print self.DiagnosisDic
        f.close()                    
            
if __name__ == "__main__":
    dicgenerator = DiagDicGenerator()
    dicgenerator._initDictionary()
    dicgenerator._getDiagDict()
    
            